package org.rency.mq.activemq.listener.handler.notify;

import com.alibaba.fastjson.JSON;
import org.rency.mq.activemq.jms.request.DefaultNotifyRequest;
import org.rency.mq.activemq.listener.handler.impl.AbstractMessageHandler;
import org.rency.mq.activemq.request.MQRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractNotifyMessageHandler<T> extends AbstractMessageHandler<T> {
	private static Logger logger = LoggerFactory.getLogger(AbstractNotifyMessageHandler.class);
	private Class<?> genericClass = Object.class;

	@Override
	public MQRequest convertToMQRequest(T request) {
		logger.debug("ConvertToMQRequest: " + request);
		if(MQRequest.class.isAssignableFrom(request.getClass())){
			return (MQRequest) request;
		}else{
			if (genericClass == null){
				return JSON.parseObject((String) request,DefaultNotifyRequest.class);
			} else {
				return (MQRequest)JSON.parseObject((String) request,genericClass);
			}
		}
	}

	protected T getMessageContent(MQRequest request) {
		DefaultNotifyRequest<?> notifyRequest = (DefaultNotifyRequest<?>) request;
		return (T)notifyRequest.getContent();
	}

	/**
	 * 娑堟伅鍐呭鐨勭被鍨嬶紙濡傛灉娑堟伅涓篔son鏍煎紡锛岄�杩囪瀛楁鎸囧畾鍙嶅簭鍒楀寲锛�
	 * @param
	 */
	public void setGenericClass(Class<?> genericClass) {
		this.genericClass = genericClass;
	}
	
}
